<?php

namespace Platform\Api;

use PhalApi\Api;
use Platform\Domain\Roles as RolesDomain;
use PhalApi\Exception\InternalServerErrorException;
use PhalApi\Exception\BadRequestException;

/**
 * 角色模块
 */
class Roles extends Api {
    
    public function getRules() {
        return array(
            'rolesList' => array(
                'page' => array('name' => 'page', 'type' => 'int', 'default' => 1, 'min' => 1, 'desc' => \PhalApi\T('page')),
                'limit' => array('name' => 'limit', 'type' => 'int', 'default' => 20, 'min' => 1, 'max' => 1000, 'desc' => \PhalApi\T('page limit')),
                'role_name' => array('name' => 'role_name', 'require' => false, 'desc' => '角色名称'),
                'sort' => array('name' => 'sort', 'require' => false, 'desc' => 'id排序值'),
            ),
            'addRoles'=> array(
                'role_name' => array('name' => 'role_name', 'require' => true, 'desc' => '角色名称'),
                'menu_list' => array('name' => 'menu_list', 'type' => 'array', 'require' => true, 'desc' => '菜单列表'),
            ),
            'rolesDetail'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '角色id'),
            ),
            'updateRoles'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '要修改的菜单id'),
                'role_name' => array('name' => 'role_name', 'require' => true, 'desc' => '角色名称'),
                'menu_list' => array('name' => 'menu_list', 'type' => 'array', 'require' => true, 'desc' => '菜单列表'),
            ),
            'deleteRoles'=> array(
                'id' => array('name' => 'id', 'require' => true, 'desc' => '角色id'),
            ),
        );
    }

    /**
     * 获取角色列表
     * @desc 获取角色列表
     */
    public function rolesList() {
        $domain = new RolesDomain();
        $where = array();
        //分页
        $page = $this->page? $this->page:1;
        $perpage = $this->limit?$this->limit:10;
        //要查询的字段
        $select="id,role_name,from_unixtime(add_time, '%Y-%m-%d %H:%i:%S') as add_time";
        //处理条件查询
        if( isset($this->role_name)&&$this->role_name != '' ){
            $where['role_name like ?'] = "%".$this->role_name."%";
        }
        //处理排序
        if( isset($this->sort) ){
            if( $this->sort == '+id' ){
                $order='id ASC';
            }elseif($this->sort == '-id'){
                $order='id DESC';
            }
        }
        $roles = $domain->rolesList($where,$select,$order,$page,$perpage);
        return $roles;
    }

    /**
     * 添加角色
     * @desc 添加角色
     */
    public function addRoles() {
        $domain = new RolesDomain();
        //列出所有需要添加的参数
        $insertData = array();
//        $insertData['project_code'] = $this->project_code;
        $insertData['role_name'] = $this->role_name;
        if( count($this->menu_list) == 0 ){
            throw new BadRequestException('请选择菜单');
        }
        $insertData['menu_list'] = $this->menu_list;
        return $domain->addRoles($insertData);
    }

    /**
     * 获取角色详情
     * @desc 获取角色详情
     */
    public function rolesDetail() {
        $domain = new RolesDomain();
        $id = $this->id;
        $select='id,role_name,delete_flag';
        return $domain->rolesDetail($id,$select);
    }

    /**
     * 编辑提交角色
     * @desc 编辑提交角色
     */
    public function updateRoles() {
        $domain = new RolesDomain();
        //列出所有需要添加的参数
        $id = $this->id;
        $updateData = array();
        $updateData['role_name'] = $this->role_name;
        if( count($this->menu_list) == 0 ){
            throw new BadRequestException('请选择菜单');
        }
        $updateData['menu_list'] = $this->menu_list;
        return $domain->updateRoles($id,$updateData);
    }

    /**
     * 删除菜单
     * @desc 删除菜单
     */
    public function deleteRoles() {
        $domain = new RolesDomain();
        $id = $this->id;
        return $domain->deleteRoles($id);
    }
}
